<?php
/**
 * Created by : PhpStorm
 * Message: 后台首页控制器
 * User: bingdi
 * Time: 2020/2/1814:42
 * motto: 现在的努力是为了小时候吹过的牛逼!
 */

namespace app\admin\controller;


use app\BaseController;
use think\facade\Db;
use think\facade\View;
use think\route\dispatch\Controller;

class Admin extends AdminBase
{
    public function index()
    {
        $model =Db::name('admin')
            ->alias('a')
            ->leftJoin('role r','a.role_id=r.id')
            ->column('a.id,a.name,a.phone,a.email,a.create_time,a.status,r.name as role_name');
        return View::fetch('admin_list', ['model' => $model]);
    }

    public function add()
    {
        if (request()->isPost()) {
            $data = input('post.');
            if ($data['password'] != $data['password2']) {
                return show(0, '两次密码输入错误');
            }
            unset($data['password2']);
            if ($data['id']) {
                if ($data['password']) {
                    $data['password'] = md5($data['password']);
                } else {
                    unset($data['password']);
                }
                $res = Db::name('admin')
                    ->where('id', $data['id'])
                    ->update($data);
            } else {
                $data['password']=md5($data['password']);
                $data['create_time']=time();
                $res = Db::name('admin')
                    ->replace()
                    ->insert($data);
            }
            if (!$res) {
                return show(0, '数据保存失败');
            }
            return show(1, '数据保存成功');
        } else {
            $id = input('id');
            $model = Db::name('admin')
                ->where('id', $id)
                ->find();
            $group = Db::name('role')->column('id,name');
            return View::fetch('admin_add', ['group' => $group, 'model' => $model]);
        }
    }

    public function role()
    {
        $model = Db::name('role')
            ->order('id', 'asc')
            ->select();
        return View::fetch('role_list', ['model' => $model]);
    }

    public function role_add()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $res['name'] = $data['name'];
            $res['describe'] = $data['describe'];
            //整理插入menu_role表的数据
            $menu = array_merge($data['user-Character-0'], $data['user-Character-0-0-0']);
            $menu_two = [];
            Db::startTrans();
            try {
                //先修改角色表
                if ($data['id']) {
                    Db::name('role')
                        ->where('id', $data['id'])
                        ->update($res);
                    $role = $data['id'];
                } else {
                    $role = Db::name('role')
                        ->insertGetId($res);
                }
                //先删除栏目表
                Db::name('menu_role')
                    ->where('role_id', $role)
                    ->delete();
                foreach ($menu as $k => $v) {
                    $menu_two[$k]['role_id'] = $role;
                    $menu_two[$k]['menu_id'] = $v;
                }
                //再新增
                Db::name('menu_role')->insertAll($menu_two);
                Db::commit();
                return show(1, '保存成功');
            } catch (\Exception $e) {
                Db::rollback();
                return show(0, '保存失败');
            }
        } else {
            $id = input('id');
            $info = Db::name('role')->where('id', $id)->find();
            $model = Db::name('menu')
                ->where(['parent_id' => 0])
                ->order('id', 'asc')
                ->select()
                ->toArray();
            foreach ($model as $k => $v) {
                $model[$k]['sub'] = Db::name('menu')
                    ->where(['parent_id' => $v['id']])
                    ->order('id', 'asc')
                    ->select()
                    ->toArray();
                if ($model[$k]['sub']) {
                    $menu_two = $model[$k]['sub'];
                    foreach ($menu_two as $item => $value) {
                        $menu_role_two = Db::name('menu_role')
                            ->where(['role_id' => $id, 'menu_id' => $v['id']])
                            ->find();
                        if ($menu_role_two) {
                            $model[$k]['sub'][$item]['true'] = 1;
                        } else {
                            $model[$k]['sub'][$item]['true'] = 2;
                        }
                    }
                }
                $menu_role = Db::name('menu_role')
                    ->where(['role_id' => $id, 'menu_id' => $v['id']])
                    ->find();
                if ($menu_role) {
                    $model[$k]['true'] = 1;
                } else {
                    $model[$k]['true'] = 2;
                }
            }
            return View::fetch('role_add', ['model' => $info, 'group' => $model]);
        }
    }

    public function permission()
    {
        $model = Db::name('menu')
            ->order('id', 'asc')
            ->select();

        return View::fetch('permission', ['model' => $model]);
    }

    public function permission_add()
    {
        if (request()->isPost()) {
            $data = input('post.');
            if ($data['id']) {
                $res = Db::name('menu')
                    ->where('id', $data['id'])
                    ->update($data);
            } else {
                $res = Db::name('menu')
                    ->insert($data);
            }
            if (!$res) {
                return show(0, '数据保存失败');
            }
            return show(1, '数据保存成功');
        } else {
            $id = input('id');
            $menu = Db::name('menu')
                ->where('id', $id)
                ->find();
            $parent = Db::name('menu')->where(['parent_id' => 0])->select();
            return View::fetch('permission_add', ['model' => $parent, 'menu' => $menu]);
        }
    }
    public function permission_del()
    {
        if (request()->isPost()) {
            $data = input('post.');
            if ($data['id']) {
                $res = Db::name('menu')
                    ->where('id', $data['id'])
                    ->update($data);
            } else {
                $res = Db::name('menu')
                    ->insert($data);
            }
            if (!$res) {
                return show(0, '数据保存失败');
            }
            return show(1, '数据保存成功');
        } else {
            $id = input('id');
            $menu = Db::name('menu')
                ->where('id', $id)
                ->find();
            $parent = Db::name('menu')->where(['parent_id' => 0])->select();
            return View::fetch('permission_add', ['model' => $parent, 'menu' => $menu]);
        }
    }
}